Skip to content

[#613] Write the log context in the file adapters - #702

Merged
armanist merged 2 commits into
quantum-php:3.2.xfrom
armanist:issue/613-logger-context
Nov 22, 2026
Merged

armanist merged 2 commits into
quantum-php:3.2.xfrom
armanist:issue/613-logger-context

Conversation

@armanist

Copy link
Copy Markdown
Collaborator

Closes #613

What

Option 2 of the ticket: the Logger stays PSR-3 (the message is string|Stringable), and the single and daily adapters write the context, which they ignored.

  • After the message, the context follows as one line of JSON: [2026-11-23 14:02:11] Info: Order paid {"order":1042}. A line without a context is unchanged.
  • A trace that is text stays on its own lines, as before; a trace that is not text (for example $e->getTrace()) goes into the JSON instead of raising an "Array to string conversion" warning.
  • An exception in the context is written as its class, message, file and line, a Stringable value as its text, a value JSON cannot encode as null; a value that throws while it is converted writes {"context":"not encodable"}, so a log call never fails because of its context.
  • The dead branch for array messages in LoggerTrait::formatMessage() is removed; logger()->info([...]) throws a TypeError as before.
  • The message adapter (debug bar) is unchanged.

Why

The docs promised array messages, but the PSR-3 signature never allowed them, and the context had no way into the log: a failed cron task lost its exception, file and line. PSR-3 puts structured data in the context, so the adapters now write it.

Breaking

The log line format changes when a context is passed: a tool that parses log lines must allow the JSON at the end. Values passed in the context, including secrets, now reach the log file. A custom adapter that uses LoggerTrait must pass formatMessage() a string.

Changed paths

  • src/Logger/Traits/LoggerTrait.php
  • tests/Unit/Logger/Adapters/SingleAdapterTest.php, DailyAdapterTest.php, MessageAdapterTest.php, tests/Unit/Logger/LoggerTest.php
  • CHANGELOG.md

Testing

  • Each case has a test with an exact check of the written line. Without the change, the context rows fail (the context is missing), a trace that is not text breaks the line, and a context value that throws escapes the logger.
  • Guards that pass before and after: no context, a trace that is text, an array message throws a TypeError, the message adapter keeps only level and message.

armanist and others added 2 commits November 23, 2026 00:22
The single and daily adapters write the context, without trace, as one line of JSON after the message; trace stays on its own lines. The message stays string|Stringable as PSR-3 defines, so the dead branch for array messages is removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ext values that fail

A trace that is not text goes into the JSON context instead of raising an Array to string conversion warning, and a context value that throws while it is converted writes a marker instead of making the log call fail.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@quantum-php quantum-php deleted a comment from coderabbitai Bot Nov 22, 2026
@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@armanist
armanist merged commit 818a83b into quantum-php:3.2.x Nov 22, 2026
6 checks passed
@armanist
armanist deleted the issue/613-logger-context branch November 22, 2026 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants